﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using OpenGado.DATA.Entities;
using OpenGado.DATA;

namespace OpenGado.Win
{
    public partial class frmReportLauncher : Form
    {
        private MySqlConnection conn;
        private MySqlDataAdapter da; 
        

        private String _connString = "user=root; password=F0ssil; server=localhost; database=OpenGado; pooling=true; connect timeout=90;";
       
        public String ConnString
        {
            get { return _connString; }
            set { _connString = value; }
        }

        public frmReportLauncher()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Populates the combo with right parameters based on the selected report
        /// </summary>
        /// <param name="selectedReport"></param>
        private void LoadParmsCombo(String selectedReport)
        {
         


            switch (selectedReport)
            {
                     
                case "Project Consumption":
                    {
                        IList<Project> projects = ProjectDataControl.Instance.GetAllProjects();
                       
                        cboParams.DataSource = null;
                        cboParams.DataSource = projects;
                        cboParams.DisplayMember = "Name";

                        lblParms.Text = "Project";

                        cboParams.Visible = true;
                        lblParms.Visible = true;
                        break;
                    }

                case "Store Products":
                    {
                      
                        IList<Storage> store = StorageDataControl.Instance.GetAllStores();
                        
                        cboParams.DataSource = null;
                        cboParams.DataSource = store;
                        cboParams.DisplayMember = "StoreName";
                        cboParams.Visible = true;
                        lblParms.Visible = true;
                        break;
                    }
                case "Orders":
                    {
                        break;
                    }

                
            }   
        }


        private DataSet GenReportData()
        {
            DataSet dsReport = new DataSet();
            String sql = string.Empty;

            switch (cboReport.Text)
            {
                case"Consumption":
                    {
                        sql = @"Select  (Select Product.product_name from product where product.productID =issue_item.productID) as product,
(Select Product.sku from product where product.productID =issue_item.productID) as SKU,
sum(issue_item.issue_quantity) as quantity,
(Select project.name from project where project.projectID = issue_item_header.projectID) as project from issue_item
Left JOIN (issue_item_header)
ON
(issue_item.issue_item_header_id = issue_item_header.issue_item_header_id)
group by projectID , productID
order by productID;";
                        break;
                    }
                case"Project Consumption":
                    {
                        sql = @"Select  (Select Product.product_name from product where product.productID =issue_item.productID) as product,
(Select Product.sku from product where product.productID =issue_item.productID) as SKU,
sum(issue_item.issue_quantity) as quantity,
(Select project.name from project where project.projectID = issue_item_header.projectID) as project from issue_item
Left JOIN (issue_item_header)
ON
(issue_item.issue_item_header_id = issue_item_header.issue_item_header_id)
Where projectID= "+getID() + @"
group by projectID , productID
order by productID;";
                        break;
                    }
                case "Products":
                    {
                        sql = @"Select * from product";
                        break;
                    }
                case "Store Products":
                    {
                        sql = @"Select product.product_name as product, product.sku, product.product_quantity as quantity, 
(Select storage.store_name from storage where store_id = product.storage_id) As store from product where storage_id=" + getID() + ";";
                        break;
                        
                    }
                case "Store Products - All":
                    {
                        sql = @"Select product.product_name as product, product.sku, product.product_quantity as quantity, 
(Select storage.store_name from storage where store_id = product.storage_id) As store from product;";
                        break;

                    }
            }

            try
            {
                conn = new MySqlConnection(ConnString);
                conn.Open();

                da = new MySqlDataAdapter(sql, conn);

                da.Fill(dsReport, "report");

            }
            catch (Exception)
            {
                
                throw;
            }
            finally
            {
                conn.Close();
            }


            return dsReport;
        }

        private string getID()
        {
            int id =0; 

            switch (cboReport.Text)
            {
                case "Project Consumption":
                    {
                        var pj = new Project();
                        pj = (Project) cboParams.SelectedItem;
                        id = pj.Projectid;
                        
                        break;

                    }

                case "Store Products":
                    {
                        var store = new Storage();
                        store = (Storage) cboParams.SelectedItem;
                        id = store.StoreId;

                        break;
                    }
            }


            return id.ToString();
        }

        private void ShowReport()
        {
            var rptDoc = new ReportDocument();

            // show report based on the selection 
            switch (cboReport.Text)
            {
                case "Consumption":
                    {
                        rptDoc = new OpenGado.Win.Reports.consumptionReport();

                        
                        break;
                    }
                case "Project Consumption":
                    {

                        rptDoc = new OpenGado.Win.Reports.consumptionReport();

                       
                        break;
                       
                    }
                case "Products":
                    {
                         rptDoc = new OpenGado.Win.Reports.ReportProducts();
                        break;
                    }
                case "Store Products":
                    {
                        rptDoc = new OpenGado.Win.Reports.storageReport();
                        break;
                    }
                case "Store Products - All":
                    {
                        rptDoc = new OpenGado.Win.Reports.storageReport();
                        break;
                    }
            }

                        var rpt = new frmReportContainer(rptDoc, GenReportData());
                        rpt.MdiParent = this.MdiParent;
                        rpt.WindowState = FormWindowState.Maximized;
                        rpt.Show();
                     


        }

        private void btnView_Click(object sender, EventArgs e)
        {
            ShowReport();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboReport_SelectedValueChanged(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            LoadParmsCombo(cboReport.Text);
            Cursor.Current = Cursors.Default;
        }

        private void ToggleDates()
        {
            if (chkPeriod.Checked)
            {
                lblTo.Visible = true;
                lblFrom.Visible = true;
                dtpStart.Visible = true;
                dtpEndDate.Visible = true;
            }
            else
            {
                lblTo.Visible = false;
                lblFrom.Visible = false;
                dtpStart.Visible = false;
                dtpEndDate.Visible = false;
            }
        }
    }
}
